<template>
  <div class="toast" :class="{ 'toast-show': visible }">
    <i class="toast-icon" :class="type === 'success' ? 'icon-success' : 'icon-warning'"></i>
    <span class="toast-text">{{ message }}</span>
  </div>
</template>

<script>
export default {
  name: 'Toast',
  props: {
    // 是否显示
    visible: {
      type: Boolean,
      default: false
    },
    // 提示消息
    message: {
      type: String,
      default: ''
    },
    // 类型：success/warning
    type: {
      type: String,
      default: 'success',
      validator: value => ['success', 'warning'].includes(value)
    },
    // 显示时长(毫秒)
    duration: {
      type: Number,
      default: 3000
    }
  },
  watch: {
    visible(newVal) {
      // 当显示时，设置自动隐藏定时器
      if (newVal) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.$emit('update:visible', false)
        }, this.duration)
      }
    }
  },
  data() {
    return {
      timer: null
    }
  },
  beforeDestroy() {
    clearTimeout(this.timer)
  }
}
</script>

<style scoped>
.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translate(-50%, -100px);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 8px;
  background-color: #ffffff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
}

.toast-show {
  transform: translate(-50%, 0);
  opacity: 1;
}

.toast-icon {
  font-size: 16px;
}

.icon-success::before {
  content: "✓";
  color: #38a169;
}

.icon-warning::before {
  content: "!";
  color: #ecc94b;
}

.toast-text {
  font-size: 14px;
  color: #2d3748;
}
</style>
